<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class Change.
 *
 * @package namespace App\Repository\Models;
 */
class Change extends Model implements Transformable
{
	use TransformableTrait;
	use SoftDeletes;

	protected $table = 'changes';
	protected $appends = ['change_type_name', 'status_name'];

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = [
		"id", "user_id", "user_name", "shop_id", "shop_name", "change_code", "change_type", "order_code", "order_id", "status", "change_money",
		"change_product", "num", "resason","created_at","updated_at", "agree_at","receipt_at", "images", "explain",];


	//退货类型映射
	public static $change_type_map = [
		0 => '退款',
		1 => '退货退款',
	];

	//退款退货状态
	public static $change_status_map = [
		0 => '买家提出申请',
		1 => '卖家同意退款',
		2 => '卖家同意退货',
		3 => '卖家拒绝',
		4 => '卖家确认收货',
		5 => '完成',
		6 => '买家取消'
	];

	public function getChangeTypeNameAttribute()
	{
		return array_get(self::$change_type_map, $this->attributes['change_type']) ?: '-';
	}

	public function getStatusNameAttribute()
	{
		return array_get(self::$change_status_map, $this->attributes['status']) ?: '-';
	}

	public function user()
	{
		return $this->hasOne(User::class, 'id', 'user_id');
	}

	public function order()
	{
		return $this->hasOne(Orders::class, 'id', 'order_id');
	}

	public function orderProducts()
	{
		return $this->hasMany(OrderProducts::class, 'order_code', 'order_code');
	}
}
